package com.das.mechanic_base.adapter.alone;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AloneNewBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.kproduce.roundcorners.RoundTextView;

import java.util.ArrayList;
import java.util.List;

public class X3AloneAnswerAdapter extends RecyclerView.Adapter<X3AloneAnswerAdapter.AloneAnswerHolder> {

    private Context mContext;

    private Drawable normal,minor,error;

    private List<AloneNewBean.ItemQuestionListBean.QuestionDetectionTempletQuestionListBean.QuestionCarMilesDeductionEntityListBean> mList;

    public X3AloneAnswerAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        normal = mContext.getResources().getDrawable(R.mipmap.x3_normal_icon);
        normal.setBounds(0, 0, normal.getMinimumWidth(), normal.getMinimumHeight());

        minor = mContext.getResources().getDrawable(R.mipmap.x3_major_icon);
        minor.setBounds(0, 0, minor.getMinimumWidth(), minor.getMinimumHeight());

        error = mContext.getResources().getDrawable(R.mipmap.x3_error_icon);
        error.setBounds(0, 0, error.getMinimumWidth(), error.getMinimumHeight());
    }

    public void changeData(List<AloneNewBean.ItemQuestionListBean.QuestionDetectionTempletQuestionListBean.QuestionCarMilesDeductionEntityListBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AloneAnswerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AloneAnswerHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_answer_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AloneAnswerHolder holder, int position) {
        String optionSingle = X3StringUtils.isEmpty(mList.get(position).optionSingle) ? "" : mList.get(position).optionSingle + ".";
        String optionDescription = mList.get(position).optionDescription;
        String status = mList.get(position).statusDesc;
        String currentStatus = mList.get(position).status;

        if ("MINOR".equals(currentStatus)) {
            holder.rv_status.setText(mContext.getString(R.string.x3_minor_notice));
            holder.rv_status.setTextColor(ContextCompat.getColor(mContext,R.color.grade_two));
            holder.rv_status.setBackgroundColor(Color.parseColor("#20FF9800"));
            holder.rv_status.setCompoundDrawables(minor,null,null,null);

        }else if ("DANGEROUS".equals(currentStatus)) {
            holder.rv_status.setText(mContext.getString(R.string.x3_error_notice));
            holder.rv_status.setTextColor(ContextCompat.getColor(mContext,R.color.grade_three));
            holder.rv_status.setBackgroundColor(Color.parseColor("#20F64458"));
            holder.rv_status.setCompoundDrawables(error,null,null,null);

        }else if ("MAJOR".equals(currentStatus)) {
            holder.rv_status.setText(mContext.getString(R.string.x3_major_notice));
            holder.rv_status.setTextColor(ContextCompat.getColor(mContext,R.color.grade_three));
            holder.rv_status.setBackgroundColor(Color.parseColor("#20F64458"));
            holder.rv_status.setCompoundDrawables(error,null,null,null);

        }else {
            holder.rv_status.setText(mContext.getString(R.string.x3_normal_notice));
            holder.rv_status.setTextColor(ContextCompat.getColor(mContext,R.color.c18));
            holder.rv_status.setBackgroundColor(Color.parseColor("#2018BE6B"));
            holder.rv_status.setCompoundDrawables(normal,null,null,null);
        }

        holder.tv_answer.setText(optionSingle + optionDescription + "");

        holder.rv_status.post(() -> {
            holder.tv_answer.setMaxWidth(holder.itemView.getWidth() - holder.rv_status.getWidth() - X3ScreenUtils.dipToPx(80,mContext));
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AloneAnswerHolder extends RecyclerView.ViewHolder {
        private TextView tv_answer;
        private RoundTextView rv_status;
        public AloneAnswerHolder(@NonNull View itemView) {
            super(itemView);
            tv_answer = itemView.findViewById(R.id.tv_answer);
            rv_status = itemView.findViewById(R.id.rv_status);
        }
    }
}
